package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.vo.account.AccountLockResultVo;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("/api/account")
@SuppressWarnings({"all"})
public class UserAccountApiController {
	@Autowired
	private UserAccountService userAccountService;







	/**
	 * 检查及扣减账户余额
	 *
	 * @param accountDeductVo
	 * @return
//	 */
//	@GuiGuLogin
//	@Operation(summary = "检查及扣减账户余额")
//	@PostMapping("/userAccount/checkAndDeduct")
//	public Result checkAndDeduct(@RequestBody AccountLockVo accountLockVo) {
//		//1.从ThreadLocal中获取用户ID
//		Long userId = AuthContextHolder.getUserId();
//		if (userId != null) {
//			accountLockVo.setUserId(userId);
//		}
//		//2.调用业务逻辑完成账户余额扣款
//		userAccountService.checkAndDeduct(accountLockVo);
//		return Result.ok();
//	}

	@PostMapping("/userAccount/updateAccount")
	void updateAccount(@RequestBody AccountLockVo accountLockVo){
		userAccountService.updateAccount(accountLockVo);
	}



	///api/account/userAccount/checkAndLock post
	@PostMapping("/userAccount/checkAndLock")
	public Result<AccountLockResultVo> checkAndLock(@RequestBody AccountLockVo accountLockVo){
		AccountLockResultVo resultVo = userAccountService.checkAndLock(accountLockVo);
		return Result.ok(resultVo);
	}


	/**
	 * 获取当前登录用户账户可用余额
	 *
	 * @return
	 */
	@GuiGuLogin
	@Operation(summary = "获取当前登录用户账户可用余额")
	@GetMapping("/userAccount/getAvailableAmount")
	public Result<BigDecimal> getAvailableAmount() {
		//获取当前登录用户ID
		Long userId = AuthContextHolder.getUserId();
		//调用业务逻辑获取账户可用金额
		BigDecimal availableAmount = userAccountService.getAvailableAmount(userId);
		return Result.ok(availableAmount);
	}
}

